﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// Project DAL
    /// </summary>
    public class ProjectDAL
    {
        public ProjectDAL()
        { }

        #region  Public Methods

        /// <summary>
        /// Add a project
        /// </summary>
        public int Add(ltModel.Model.Project model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_Project(");
            strSql.Append("ProjectName,Organizer,ServiceLocation,ProjectContent,RecruitmentLocation,RecruitmentAmount,RegistrationRequirement,RegistrationDeadLine,Contact,Attachment,ProjectStreetID,ProjectTypeID");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.ProjectName + "',");
            strSql.Append("'" + model.Organizer + "',");
            strSql.Append("'" + model.ServiceLocation + "',");
            strSql.Append("'" + model.ProjectContent + "',");
            strSql.Append("'" + model.RecruitmentLocation + "',");
            strSql.Append("'" + model.RecruitmentAmount + "',");
            strSql.Append("'" + model.RegistrationRequirement + "',");
            strSql.Append("'" + model.RegistrationDeadLine + "',");
            strSql.Append("'" + model.Contact + "',");
            strSql.Append("'" + model.Attachment + "',");
            strSql.Append(model.ProjectStreetID + ",");
            strSql.Append(model.ProjectTypeID);
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// Update a project
        /// </summary>
        public bool Update(ltModel.Model.Project model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_Project set ");
            if (!string.IsNullOrEmpty(model.ProjectName))
                strSql.Append("ProjectName='" + model.ProjectName + "',");
            if (!string.IsNullOrEmpty(model.Organizer))
                strSql.Append("Organizer='" + model.Organizer + "',");
            if (!string.IsNullOrEmpty(model.ServiceLocation))
                strSql.Append("ServiceLocation='" + model.ServiceLocation + "',");
            if (!string.IsNullOrEmpty(model.ProjectContent))
                strSql.Append("ProjectContent='" + model.ProjectContent + "',");
            if (!string.IsNullOrEmpty(model.RecruitmentLocation))
                strSql.Append("RecruitmentLocation='" + model.RecruitmentLocation + "',");
            if (!string.IsNullOrEmpty(model.RecruitmentAmount))
                strSql.Append("RecruitmentAmount='" + model.RecruitmentAmount + "',");
            if (!string.IsNullOrEmpty(model.RegistrationRequirement))
                strSql.Append("RegistrationRequirement='" + model.RegistrationRequirement + "',");
            if (model.RegistrationDeadLine != DateTime.MinValue)
                strSql.Append("RegistrationDeadLine='" + model.RegistrationDeadLine + "',");
            if (!string.IsNullOrEmpty(model.Contact))
                strSql.Append("Contact='" + model.Contact + "',");
            if (!string.IsNullOrEmpty(model.Attachment))
                strSql.Append("Attachment='" + model.Attachment + "',");
            if (model.ProjectStreetID > 0)
                strSql.Append("ProjectStreetID=" + model.ProjectStreetID + ",");
            if (model.ProjectTypeID > 0)
                strSql.Append("ProjectTypeID=" + model.ProjectTypeID);

            strSql.Append(" where [ProjectID]=" + model.ProjectID);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Delete project by id
        /// </summary>
        public bool Delete(int projectID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_Project ");
            strSql.Append(" where ProjectID=" + projectID);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /*
        /// <summary>
        /// 删除数据
        /// </summary>
        public bool Delete(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_xmsq ");
            strSql.Append(" where [id] in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 审核消审操作
        /// </summary>
        /// <param name="ids">ID</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        public bool UpdateZT(string ids, string zt)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_xmsq set ");
            strSql.Append(" zt = " + zt + "");
            strSql.Append(" where [id] in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 公开取消公开操作
        /// </summary>
        /// <param name="ids">ID</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        public bool UpdatePublic(string ids, string zt)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_xmsq set ");
            strSql.Append(" inpublic = " + zt + "");
            strSql.Append(" where [id] in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }*/

        /// <summary>
        /// Get project by id
        /// </summary>
        public ltModel.Model.Project GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from lt_Project ");
            strSql.Append(" where ProjectID=" + id);
            ltModel.Model.Project model = null;
            DataSet ds = DataObject.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                model = new Model.Project();
                model.ProjectID = id;
                model.Attachment = ds.Tables[0].Rows[0]["Attachment"].ToString();
                model.Contact = ds.Tables[0].Rows[0]["Contact"].ToString();
                model.Organizer = ds.Tables[0].Rows[0]["Organizer"].ToString();
                model.ProjectContent = ds.Tables[0].Rows[0]["ProjectContent"].ToString();
                model.ProjectName = ds.Tables[0].Rows[0]["ProjectName"].ToString();
                model.ProjectStreetID = Convert.ToInt32(ds.Tables[0].Rows[0]["ProjectStreetID"]);
                model.ProjectTypeID = Convert.ToInt32(ds.Tables[0].Rows[0]["ProjectTypeID"]);
                model.RecruitmentAmount = ds.Tables[0].Rows[0]["RecruitmentAmount"].ToString();
                model.RecruitmentLocation = ds.Tables[0].Rows[0]["RecruitmentLocation"].ToString();
                model.RegistrationDeadLine = Convert.ToDateTime(ds.Tables[0].Rows[0]["RegistrationDeadLine"]);
                model.RegistrationRequirement = ds.Tables[0].Rows[0]["RegistrationRequirement"].ToString();
                model.ServiceLocation = ds.Tables[0].Rows[0]["ServiceLocation"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Get project list
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM lt_Project");

            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// Get project list by limit
        /// <param name="TopNum">limit</param>
        /// <param name="strWhere">condition</param>
        /// </summary>
        public DataSet GetList(string TopNum, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (TopNum.Trim() != "" && Int32.Parse(TopNum) > 0)
                strSql.Append(" top " + TopNum + " ");
            strSql.Append(" *");
            strSql.Append(" FROM lt_Project ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// Update project status.
        /// </summary>
        /// <param name="projectID"></param>
        /// <param name="approved"></param>
        /// <returns></returns>
        public bool UpdateStatus(string projectIDs, string approved)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_Project set approved = ");
            strSql.Append(approved);
            strSql.Append(" where ProjectID in (" + projectIDs + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        #endregion
    }
}